package com.joysuch.wwyt.openapi.controllers.controllerV1;

import com.joysuch.wwyt.bp.entity.BpDepart;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiCoreException;
import com.joysuch.wwyt.dzhapi.dto.BuildDataDto;
import com.joysuch.wwyt.dzhapi.service.ApiTokenVerifyService;
import com.joysuch.wwyt.openapi.constants.ApiStatus;
import com.joysuch.wwyt.openapi.controllers.controllerV1.req.*;
import com.joysuch.wwyt.openapi.controllers.controllerV1.vo.ApiPagePropertyDataVo;
import com.joysuch.wwyt.openapi.controllers.controllerV1.vo.ApiResultData;
import com.joysuch.wwyt.openapi.controllers.controllerV1.vo.ApiV1BpDepartTreeListVo;
import com.joysuch.wwyt.openapi.exception.ApiV1ExceptionCodeMsg;
import com.joysuch.wwyt.openapi.result.ApiV1Result;
import com.joysuch.wwyt.openapi.services.serviceV1.ApiV1BpDepartService;
import com.joysuch.wwyt.openapi.services.serviceV1.ApiV1TokenVerifyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import retrofit2.http.POST;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author: JamesZY
 * @date: 2021/8/24
 * @description:组织接口
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/depart")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ApiV1BpDepartController {
    private final ApiV1BpDepartService service;
    private final ApiTokenVerifyService verifyService;

    @PostMapping("/batchAdd")
    public ApiV1Result batchAdd(@RequestBody @Valid ApiV1DataReq<ApiV1BpDepartAddReq> req, HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
        } catch (ApiV1ExceptionCodeMsg e) {
            return ApiV1Result.error(e.getCode(), e.getMessage());
        }
        List<ApiResultData> dataList = service.add(req.getData(),buildDataDto);
        return ApiV1Result.success(dataList);
    }

    @PostMapping("/batchDelete")
    public ApiV1Result batchDelete(@RequestBody @Valid ApiV1DataReq<String> req, HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
        } catch (ApiV1ExceptionCodeMsg e) {
            return ApiV1Result.error(e.getCode(), e.getMessage());
        }
        List<ApiResultData> dataList = service.deleteByIds(req.getData(),buildDataDto);
        return ApiV1Result.success(dataList);
    }

    @PostMapping("/update")
    public ApiV1Result update(@RequestBody @Valid ApiV1BpDepartUpdateReq req, HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
            service.update(req,buildDataDto);
        } catch (ApiV1ExceptionCodeMsg e) {
            return new ApiV1Result(e.getCode(), ApiStatus.ERROR, e.getMessage(), null);
        }
        return ApiV1Result.success();
    }

    @PostMapping("/list")
    public ApiV1Result list(@RequestBody @Valid ApiV1BpDepartQueryListReq req, HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
        } catch (ApiV1ExceptionCodeMsg e) {
            return ApiV1Result.error(e.getCode(), e.getMessage());
        }
        ApiPagePropertyDataVo list = service.list(req,buildDataDto);
        return ApiV1Result.success(list);
    }


    @PostMapping("/list/tree")
    public ApiV1Result treeList(@RequestBody @Valid ApiV1BpDepartTreeListReq req, HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
        } catch (ApiV1ExceptionCodeMsg e) {
            return ApiV1Result.error(e.getCode(), e.getMessage());
        }
        List<ApiV1BpDepartTreeListVo> treeList = service.treeList(req,buildDataDto);
        return ApiV1Result.success(treeList);
    }
}
